package vn.vnu.uet.university;

import vn.vnu.uet.university.bean.DatabaseHandler;
import vn.vnu.uet.university.bean.User;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Login  extends Activity{
//	AlertDialog.Builder alertbox = new Builder(Login.this);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		Token.instance();
		Token.clear();
		Token.instance().user = new User();
		final EditText userName = (EditText) findViewById(R.id.user);
		final EditText password = (EditText) findViewById(R.id.password);
			
		final DatabaseHandler db = new DatabaseHandler(Login.this);

		Button btn = (Button) findViewById(R.id.sign_in_button);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					User temp = db.getUserByUserName(userName.getText().toString());
					if (temp.password.equals(password.getText().toString())) {
						password.setText("");
						Token.instance().user = temp;
						ViewController.showMainActivity(Login.this);
					}
					else{
						AlertDialog.Builder alertbox = new Builder(Login.this);
						alertbox.setMessage("Sai mật khẩu!").show();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
											
			}
		});
	}
}
